package com.cxb.adapter;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.cxb.R;
import com.cxb.entity.AccessPoint;

import java.util.List;

/**
 * Author jerry
 * date: 2016/8/6.
 */
public class WifiAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private List<AccessPoint> mAccessPoints;

	public WifiAdapter(Context context, List<AccessPoint> accessPoints){
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mAccessPoints = accessPoints;
	}

	public void updateAccesspoint(List<AccessPoint> accessPoints){
		mAccessPoints = accessPoints;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mAccessPoints.size();
	}

	@Override
	public Object getItem(int position) {
		return mAccessPoints.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int positon, View convertView, ViewGroup viewGroup) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.wifi_item_layout, null);
			holder = new ViewHolder();
			holder.mWifiSignal = (ImageView) convertView.findViewById(R.id.wifi_singal);
			holder.mWifiFree = (ImageView) convertView.findViewById(R.id.wifi_lock);
			holder.mWifiSSID = (TextView) convertView.findViewById(R.id.wifi_ssid);
			convertView.setTag(holder);
		}
		else{
			holder = (ViewHolder)convertView.getTag();
		}

		AccessPoint accessPoint = mAccessPoints.get(positon);
		holder.mWifiSSID.setText(accessPoint.SSID);
		if(accessPoint.isFree()){//如果是不需要密码的，干脆不显示锁图标
			holder.mWifiFree.setVisibility(View.INVISIBLE);
		}else {
			holder.mWifiFree.setVisibility(View.VISIBLE);
		}
		updateSingalIcon(accessPoint, holder.mWifiSignal);
		return convertView;
	}

	public static void updateSingalIcon(AccessPoint accessPoint, ImageView signalView){
		int signalLevel = WifiManager.calculateSignalLevel(accessPoint.level, 5);
		switch (signalLevel){
			case 0:{
				signalView.setImageResource(R.drawable.wifi_unlock_0);
				break;
			}

			case 1:{
				signalView.setImageResource(R.drawable.wifi_unlock_1);
				break;
			}

			case 2:{
				signalView.setImageResource(R.drawable.wifi_unlock_2);
				break;
			}

			case 3:{
				signalView.setImageResource(R.drawable.wifi_unlock_3);
				break;
			}

			case 4:{
				signalView.setImageResource(R.drawable.wifi_unlock_4);
				break;
			}
		}
	}
	class ViewHolder{
		ImageView mWifiSignal;
		ImageView mWifiFree;
		TextView mWifiSSID;
	}
}
